//
//  AnchorPointViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/4.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "AnchorPointViewController.h"

static CGFloat kImageWidth = 120.f;
static CGFloat kAnchorPointWidth = 8.f;
@interface AnchorPointViewController ()
@property (weak, nonatomic) IBOutlet UILabel *xLabel;
@property (weak, nonatomic) IBOutlet UILabel *yLabel;
@property (weak, nonatomic) IBOutlet UIStepper *xStepper;
@property (weak, nonatomic) IBOutlet UIStepper *yStepper;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (strong, nonatomic) UIView *anchorPointView;
@end

@implementation AnchorPointViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.anchorPointView = [[UIView alloc] init];
    self.anchorPointView.backgroundColor = [UIColor blueColor];
    self.anchorPointView.layer.masksToBounds = YES;
    self.anchorPointView.layer.cornerRadius = kAnchorPointWidth / 2.f;
    [self.imageView addSubview:self.anchorPointView];
    
    
    self.imageView.bounds = CGRectMake(0, 0, kImageWidth, kImageWidth);
    self.imageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2.f, 104 + kImageWidth / 2.f);
    
    self.anchorPointView.bounds = CGRectMake(0, 0, kAnchorPointWidth, kAnchorPointWidth);
    self.anchorPointView.center = CGPointMake(CGRectGetWidth(self.imageView.frame) / 2.f, CGRectGetHeight(self.imageView.frame) / 2.f);
}


- (IBAction)xChanged:(UIStepper *)sender {
    
    self.xLabel.text = @(sender.value).stringValue;
    self.imageView.layer.anchorPoint = CGPointMake(sender.value, self.yLabel.text.floatValue);
    
    CGPoint p = self.anchorPointView.center;
    p.x = sender.value * CGRectGetWidth(self.imageView.frame);
    self.anchorPointView.center = p;
}

- (IBAction)yChanged:(UIStepper *)sender {
    
    self.yLabel.text = @(sender.value).stringValue;
    
    self.imageView.layer.anchorPoint = CGPointMake(self.xLabel.text.floatValue, sender.value);
    
    CGPoint p = self.anchorPointView.center;
    p.y = sender.value * CGRectGetHeight(self.imageView.frame);
    self.anchorPointView.center = p;
}

- (IBAction)rotate:(id)sender {
    
    [UIView animateWithDuration:.3f animations:^{
        self.imageView.layer.affineTransform = CGAffineTransformRotate(self.imageView.layer.affineTransform, M_PI_2);
    }];
}

- (IBAction)scal:(id)sender {
    [UIView animateWithDuration:.3f animations:^{
        self.imageView.layer.affineTransform = CGAffineTransformScale(self.imageView.layer.affineTransform, 1.5, 1.5);
    }];
}

- (IBAction)reset:(id)sender {
    self.xStepper.value = .5f;
    self.yStepper.value = .5f;
    
    self.imageView.center = CGPointMake(CGRectGetMidX(self.view.frame), 104 + kImageWidth / 2.f);
    self.imageView.layer.affineTransform = CGAffineTransformIdentity;
    
    [self xChanged:self.xStepper];
    [self yChanged:self.yStepper];
}
@end
